package com.yq.light.knowledgeDB.code.controller;

import com.yq.light.commons.code.entity.CommonResult;
import com.yq.light.commons.code.entity.KnowledgeDb;
import com.yq.light.commons.code.entity.KnowledgeFile;
import com.yq.light.knowledgeDB.code.service.KnowledgeDBService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@Controller
@RequestMapping("/knowledgeDB")
@Slf4j
public class KnowledgeDBController {

    @Autowired
    private KnowledgeDBService knowledgeDBService;

    /**
     * select用户的知识库列表，并返回数据给前端
     * @param ownerAccount
     * @return
     */
    @RequestMapping("/fetchUserKnowledgeDbList")
    @ResponseBody
    public CommonResult fetchUserKnowledgeDbList(@RequestParam("ownerAccount") String ownerAccount){
        String result = null;
        try{
            List<KnowledgeDb> list = knowledgeDBService.fetchUserKnowledgeDbList(ownerAccount);
            if(list != null){
                return new CommonResult(200,"请求成功",list);
            }
        }catch (Exception e){
            log.error("KnowledgeDBController--"+"fetchUserKnowledgeDbList--"+String.valueOf(e));
        }

        return new CommonResult(200,"请求失败",result);
    }

    /**
     * select用户的知识库中的文件列表，并返回数据给前端
     * @param ownerAccount
     * @param fileTitle
     * @return
     */
    @RequestMapping("/fetchUserKnowledgeDbFileList")
    @ResponseBody
    public CommonResult fetchUserKnowledgeDbFileList(@RequestParam("ownerAccount") String ownerAccount
            ,@RequestParam("fileTitle") String fileTitle){
        String result = null;
        try{
            List<KnowledgeFile> list = knowledgeDBService.fetchUserKnowledgeDbFileList(ownerAccount,fileTitle);
            if(list != null){
                return new CommonResult(200,"请求成功",list);
            }
        }catch (Exception e){
            log.error("KnowledgeDBController--"+"fetchUserKnowledgeDbFileList--"+String.valueOf(e));
        }

        return new CommonResult(200,"请求失败",result);
    }

    /**
     * insert新建一个知识库
     * @param knowledgeDb
     * @return
     */
    @RequestMapping("/newKnowledgeDb")
    @ResponseBody
    public CommonResult newKnowledgeDb(@RequestBody KnowledgeDb knowledgeDb){
        int result = 0;
        try{
            result = knowledgeDBService.newKnowledgeDb(knowledgeDb);
            if(result > 0){
                return new CommonResult(200,"请求成功",result);
            }
        }catch (Exception e){
            log.error("KnowledgeDBController--"+"newKnowledgeDb--"+String.valueOf(e));
        }

        return new CommonResult(200,"请求失败",result);
    }

    /**
     * insert新建一个知识库文件
     * @param knowledgeFile
     * @return
     */
    @RequestMapping("/newKnowledgeDbFile")
    @ResponseBody
    public CommonResult newKnowledgeFile(@RequestBody KnowledgeFile knowledgeFile){
        int result = 0;
        try{
            result = knowledgeDBService.newKnowledgeFile(knowledgeFile);
            if(result > 0){
                return new CommonResult(200,"请求成功",result);
            }
        }catch (Exception e){
            log.error("KnowledgeDBController--"+"newKnowledgeFile--"+String.valueOf(e));
        }

        return new CommonResult(200,"请求失败",result);
    }

    /**
     * update知识库中的文件
     * @param knowledgeFile
     * @return
     */
    @RequestMapping("/updateKnowledgeDbFile")
    @ResponseBody
    public CommonResult updateKnowledgeDb(@RequestBody KnowledgeFile knowledgeFile){
        String result = null;
        try{
            result = knowledgeDBService.updateKnowledgeDbFile(knowledgeFile);
            if(result != null){
                return new CommonResult(200,"请求成功",result);
            }
        }catch (Exception e){
            log.error("KnowledgeDBController--"+"newKnowledgeFile--"+String.valueOf(e));
        }
        return new CommonResult(200,"请求失败",result);
    }
}
